<!--车辆管理页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">车辆维修</span>
    <el-button @click="beforeAddVehicleRepair" type="primary" style="float:right;margin-top:13px;" >录入维修车辆</el-button>
  </div>
  <!-- 新增录入维修车辆弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;" :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="送修人">
            <el-input  readonly :value="user.username"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修车辆">
            <el-select placeholder="请选择维修车辆车牌号" v-model="vehicleRepair.vehicleId" >
              <el-option v-for="item in vehicleList" :label="item.license" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修项目">
            <el-select placeholder="请选择" v-model="vehicleRepair.item">
              <el-option  v-for="item in repairItemList" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修地点">
            <el-select placeholder="请选择"   v-model="vehicleRepair.addr">
              <el-option  v-for="item in repairAddressList" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修材料">
            <el-select placeholder="请选择" v-model="vehicleRepair.material" >
              <el-option  v-for="item in repairMaterialList" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修费用">
            <el-input placeholder="请输入"  v-model="vehicleRepair.fee"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD" v-model="vehicleRepair.repairTime"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修状态">
            <el-radio-group v-model="vehicleRepair.status" >
              <el-radio value="1">维修中</el-radio>
              <el-radio value="0">已完成</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveRepair">录入</el-button>
    </template>
  </el-dialog>
  <!-- 车辆搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="维修车辆">
        <el-input placeholder="请输入车牌号" style="width:220px;" v-model="searchRepair.license"></el-input>
      </el-form-item>
      <el-form-item label="送修人员" >
        <el-input placeholder="请输入送修人员姓名" style="width:220px;" v-model="searchRepair.username"></el-input>
      </el-form-item>
      <el-form-item label="维修状态" >
        <el-select placeholder="请选择维修状态" style="width: 220px;" v-model="searchRepair.status">
          <el-option label="维修中" value="1"></el-option>
          <el-option label="已完成" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadRepair">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 车辆列表 -->
  <el-card style="margin:20px;" >
    <el-table :data="repairArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="license" label="维修车辆" align="center"/>
      <el-table-column prop="item" label="维修项目" align="center" :formatter="itemFormatter"/>
      <el-table-column prop="username" label="送修人员" align="center"/>
      <el-table-column prop="addr" label="维修地点" align="center" :formatter="addrFormatter"/>
      <el-table-column prop="repairTime" label="维修时间" align="center"/>
      <el-table-column prop="fee" label="维修费用" align="center"/>
      <el-table-column prop="material" label="维修材料" align="center" :formatter="materialFormatter"/>
      <el-table-column prop="status" label="维修状态" align="center" :formatter="statusFormatter" />

      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editRepair(scope.row.id)" >编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteRepair(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";
import index from "vuex";


// 获取当前用户
const user=ref(getUser());
//控制新增车辆弹窗是否显示
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('');
// 定义数组保存可维修车辆
const vehicleList=ref([]);
// 定义数组保存维修项目
const repairItemList=ref([]);
// 定义数组保存维修地点
const repairAddressList=ref([]);
//定义数组保存维修材料项
const repairMaterialList=ref([]);
// 定义对象保存维修车辆信息
const vehicleRepair=ref({

  vehicleId:'',
  item:'',
  addr:'',
  material:'',
  fee:'',
  repairTime:'',
  status:'',
  username:user.value.username,
  userId:user.value.id
});

// 定义方法新增车辆维修
const beforeAddVehicleRepair=()=>{
  dialogTitle.value='新增车辆维修';
  dialogVisible.value=true;

  axios.get(BASE_URL+'/v1/vehicle/select?status=1')
      .then((response)=>{
        if (response.data.code==2000){
          vehicleList.value=response.data.data;
        }else {
          ElMessage.error(response.data.msg);
        }
      })
};
// 定义取消录入维修车辆方法
const handleClose=()=>{
  if (confirm("您确认取消录入维修车辆吗？")){
    vehicleRepair.value = {
      vehicleId: '',
      item: '',
      addr: '',
      material: '',
      fee: '',
      repairTime: '',
      status: '',
      username:user.value.username,
      userId:user.value.id,
    };
    dialogVisible.value=false;
  }
};

// 保存录入维修车辆方法
const saveRepair=()=>{
  let data=qs.stringify(vehicleRepair.value)
  console.log(data);
  axios.post(BASE_URL+'/v1/repair/save',data)
      .then((response)=>{
        if (response.data.code==2000){
          ElMessage.success("保存成功");
          vehicleRepair.value = {
            vehicleId: '',
            item: '',
            addr: '',
            material: '',
            fee: '',
            repairTime: '',
            status: '',
            username:user.value.username,
            userId:user.value.id,
          };
          dialogVisible.value=false;
          loadRepair();

        }else {
          ElMessage.error(response.data.msg);
        }
      })
};




// 定义对象保存搜索条件
const searchRepair=ref({
  license:'',
  username:'',
  status:''

});
// 定义数组，保存查询对象
const repairArr=ref([]);

// 定义查询方法
const loadRepair=()=>{
  let data=qs.stringify(searchRepair.value)
  console.log("参数："+data);
  axios.get(BASE_URL+'/v1/repair/select?'+data)
      .then((response)=>{
        if (response.data.code==2000){
          repairArr.value=response.data.data;
        }else {
          ElMessage.error(response.data.msg)
        }

      })
};

// 页面一开始就加载维修数据
onMounted(()=>{
  loadRepair();
  loadDictOption(repairItemList,'repair_item');
  loadDictOption(repairAddressList,'repair_address');
  loadDictOption(repairMaterialList,'repair_material');
})

// 定义重置方法
const resetSearch=()=>{
  searchRepair.value={
    license:'',
    username:'',
    status:''
  }
   loadRepair();
};

// 编辑维修单
const editRepair=(id)=>{
  dialogTitle.value='编辑维修车辆';
  dialogVisible.value=true;
  axios.get(BASE_URL+'/v1/repair/select?id='+id)
      .then((response)=>{
        if (response.data.code==2000){
          vehicleRepair.value=response.data.data[0];
        }else {
          ElMessage.error(response.data.msg);
        }
      })
};

// 删除维修车辆单
const deleteRepair=(id)=>{
  if (confirm('确认删除？')){
    axios.post(BASE_URL+'/v1/repair/delete/'+id)
        .then((response)=>{
          if (response.data.code==2000){
            ElMessage.success('删除成功！');
            loadRepair();

          }else {
            ElMessage.error(response.data.msg);
          }
        });
  }
};

// 显示格式
const statusFormatter=(row,column,cellValue,index)=>{
  if (cellValue==0){
    cellValue="已完成";
  }else if (cellValue==1){
    cellValue="维修中"
  }
  return cellValue;
};

const itemFormatter=(row,column,cellValue,index)=>{
  if(!repairItemList.value){
    console.log('测试1'+repairItemList.value)
    return cellValue;
  }
  for (let item of repairItemList.value){
    if (item.value==cellValue){
      console.log('测试2'+repairItemList.value)
      cellValue=item.label;
    }
  }
  return  cellValue;
};

const materialFormatter=(row,column,cellValue,index)=>{
  if( !repairMaterialList.value ){
    return cellValue;
  }
  for (let item of repairMaterialList.value){
    if ( item.value==cellValue ){
      cellValue=item.label;
    }
  }
  return  cellValue;
};

const addrFormatter=(row,column,cellValue,index)=>{
  if( !repairAddressList.value ){
    return cellValue;
  }
  for (let item of repairAddressList.value){
    if ( item.value==cellValue){
      cellValue=item.label;
    }
  }
  return  cellValue;
}

















</script>